package cn.yxl.org.controller;

import cn.yxl.basic.utils.AjaxResult;
import cn.yxl.basic.utils.PageList;
import cn.yxl.org.domain.Department;
import cn.yxl.org.query.DepartmentQuery;
import cn.yxl.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

   //
    @GetMapping
    @ApiOperation(value = "查询所有" )
    public List<Department> qeuryAll(){
        return departmentService.queryAll();
    }

    //基于id查询  get   http://localhost:8080/dept/1
    @GetMapping("/{id}")
    public Department queryById(@PathVariable("id") Long id){
        return departmentService.queryById(id);
    }

    //分页查询+条件查询 post  http://localhost:8080/dept
    @PostMapping
    public PageList<Department> queryByPage(@RequestBody DepartmentQuery query){
        return departmentService.queryByPage(query);
    }



    //新增和修改 put  http://localhost:8080/dept
    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody Department department){
        try {
            if(department.getId() == null){
                departmentService.save(department);
            }else {
                departmentService.update(department);
            }
            return  new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }


    //删除  delete   http://localhost:8080/dept/1
    @DeleteMapping("/{id}")
    public AjaxResult deleteById(@PathVariable("id") Long id){
        try {
            departmentService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }
}
